package com.moyou.weichat.view.ui;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.widget.EditText;


import com.moyou.weichat.R;
import com.moyou.weichat.dapter.PageSetAdapter;
import com.moyou.weichat.view.ui.filter.QqFilter;
import com.sj.emoji.EmojiBean;

import java.io.IOException;


public class QqUtils extends SimpleCommonUtils {

    public void initEmoticonsEditText(EmoticonsEditText etContent) {
        super.initEmoticonsEditText(etContent);
        etContent.addEmoticonFilter(new QqFilter());
    }

    public static EmoticonClickListener getCommonEmoticonClickListener(final EditText editText) {
        return (o, actionType, isDelBtn) -> {
            if (isDelBtn) {
                QqUtils.delClick(editText);
            } else {
                if (o == null) {
                    return;
                }
                if (actionType == Constants.EMOTICON_CLICK_TEXT) {
                    String content = null;
                    if (o instanceof EmojiBean) {
                        content = ((EmojiBean) o).emoji;
                    } else if (o instanceof EmoticonEntity) {
                        content = ((EmoticonEntity) o).getContent();
                    }

                    if (TextUtils.isEmpty(content)) {
                        return;
                    }
                    int index = editText.getSelectionStart();
                    Editable editable = editText.getText();
                    editable.insert(index, content);
                }
            }
        };
    }


    public PageSetAdapter getCommonAdapter(Context context, EmoticonClickListener emoticonClickListener) {
        PageSetAdapter pageSetAdapter = super.getCommonAdapter(context, emoticonClickListener);
        addXhsPageSetBQ(pageSetAdapter, context, emoticonClickListener);
     //   addXhsPageSetEntity(pageSetAdapter, context, emoticonClickListener);
        addXhsPageGifSetEntity(pageSetAdapter, context, emoticonClickListener);
        addXhsPageGifXZSetEntity(pageSetAdapter, context, emoticonClickListener);
        return pageSetAdapter;
    }


    public static EmoticonDisplayListener<Object> getEmoticonDisplayListener(final EmoticonClickListener emoticonClickListener) {
        return (position, parent, viewHolder, object, isDelBtn) -> {
            final EmoticonEntity emoticonEntity = (EmoticonEntity) object;
            if (emoticonEntity == null && !isDelBtn) {
                return;
            }
            viewHolder.ly_root.setBackgroundResource(R.drawable.bg_emoticon);
            if (isDelBtn) {
                viewHolder.iv_emoticon.setImageResource(R.mipmap.icon_del);
            } else {
                try {
                    ImageLoader.getInstance(viewHolder.iv_emoticon.getContext()).displayImage(emoticonEntity.getIconUri(), viewHolder.iv_emoticon);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            viewHolder.rootView.setOnClickListener(v -> {
                if (emoticonClickListener != null) {
                    emoticonClickListener.onEmoticonClick(emoticonEntity, Constants.EMOTICON_CLICK_TEXT, isDelBtn);
                }
            });
        };
    }

}
